/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
 var spiralOrder = function(matrix) {
    let top = 0
    let bottom = matrix.length - 1
    let left = 0
    let right = matrix[0] && matrix[0].length - 1
    let res = []
    while(top <= bottom || left <= right){
        for(let i = left;i <= right;i++){
            res.push(matrix[top][i])
        }
        top++
        if(top > bottom) break
        for(let i = top;i <= bottom;i++){
            res.push(matrix[i][right])
        }
        right--
        if(left > right) break
        for(let i = right;i >= left;i--){
            res.push(matrix[bottom][i])
        }
        bottom--
        if(top > bottom) break
        for(let i = bottom;i >= top;i--){
            res.push(matrix[i][left])
        }
        left++
        if(left > right) break
    }
    return res
};